import moment from 'moment';
var CryptoJS = require("crypto-js");
import { Base64 } from 'js-base64';
const queryString = require('query-string');

export function dealParams(o) {
	for(var key in o) {
		if((!o[key] && o[key] != 0) || (o[key] == 'undefined')) {
		  delete o[key]
		}
	 }

	console.log('处理后的请求参数',o)

	var date = moment();

	var publicParams = {
		"nonce": RndNum(6),
		"requestId": "4001" + date.format('YYYYMMDDmmss') + "10" + RndNum(6),
		"timeStamp": date.format('YYYY-MM-DDHH:mm:ss.311'),
	};
	// for (let key in o) {
	// 	publicParams[key] = o[key];
	// }
	let requestParam = { ...publicParams, ...o }

	// console.log('密钥：',localStorage.getItem('secretKey'));
	// console.log('token：',localStorage.getItem('token'));

	// console.log('签名参数=====', requestParam);
	var sign = getSign(requestParam);
	// console.log('签名=====', sign);

	requestParam.signature = sign;
	requestParam.timeStamp = date.format('YYYY-MM-DD HH:mm:ss.311');

	// console.log('请求包加密前的值：', requestParam)

	// let beforeSign = 'appType=1nonce=254162requestId=400220180427160603336123timeStamp=2017-06-27 13:59:10.311userId=600000000000000000169ce5a6b940eb1d14acb76f9c8563020'
	// let fake = {"timeStamp":"2017-06-27 13:59:10.311","signature":"6EFD1604DBC4D125E4BF1E74C58F7B8EA68C3691","requestId":"400220180427160603336123","appType":1,"nonce":"254162","userId":6000000000000000001}
	// let fakekey = '925f90b678902751e754338f7138c29f';
	// var fakeHex = CryptoJS.enc.Utf8.parse(fakekey);
	// var data1 = CryptoJS.TripleDES.encrypt(CryptoJS.enc.Utf8.parse(JSON.stringify(fake)), fakeHex, {
	// 	iv: CryptoJS.enc.Utf8.parse('01234567'),
	// 	mode: CryptoJS.mode.ECB,
	// 	padding: CryptoJS.pad.Pkcs7
	// });
	// let result1 = data1.toString()
	// console.log('模拟加密后的参数00000=========', '' + result1);

	var secretKey = localStorage.getItem("secretKey");


	// console.log('请求包加密前的值',JSON.stringify(requestParam))
	//3des 
	var keyHex = CryptoJS.enc.Utf8.parse(secretKey);
	var data = CryptoJS.TripleDES.encrypt(CryptoJS.enc.Utf8.parse(JSON.stringify(requestParam)), keyHex, {
		iv: CryptoJS.enc.Utf8.parse('01234567'),
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	// console.log('请求包加密后的值：', data.toString());
	// return data;
	var result = data.toString();
	return result;
}

function RndNum(n) {
	var rnd = "";
	for (var i = 0; i < n; i++)
		rnd += Math.floor(Math.random() * 10);
	return rnd;
}

function getSign(o) {
	var p = sort(o);
	var t = '';
	for (let key in p) {
		var okey = o[key];
		var tt = JSON.stringify(okey) || '';
		// if tt 是个json

		if (tt && Array.isArray(okey) && key != 'catIdList') {
			var arr = [];
			for (let itemObj of okey) {
				arr.push(specialSort(itemObj));
				// console.log('type2');
				// arr.push(sort(itemObj));
				// console.log('type1');
			}
			tt = JSON.stringify(arr);
			// console.log('tt1', tt);
		}
		// console.log('tt',tt);
		t = t + key + '=' + tt.replace(/\"/gi, "");
	}
	t = t + localStorage.getItem("token");
	t = t.replace(/\s/g, "");
	// console.log('签名之前的值：', t);


	var sha1 = CryptoJS.SHA1(t).toString();
	var testsha1 = CryptoJS.SHA1(o).toString()
	// var sha1 = hex_sha1(t); 
	// console.log('签名后得到的值=====',sha1.toUpperCase());
	return sha1.toUpperCase();
}

function sort(o) {
	var newkey = Object.keys(o).sort();
	var newObj = {};
	for (var i = 0; i < newkey.length; i++) {
		newObj[newkey[i]] = o[newkey[i]];
	}
	return newObj;
}

function specialSort(o) {
	var newkey = Object.keys(o).sort();
	// console.log('newkey v3', newkey);
	var newObj = {};
	if (newkey.length <= 3) {
		for (var i = newkey.length - 1; i >= 0; i--) {
			newObj[newkey[i]] = o[newkey[i]];
		}
	} else {
		newObj[newkey[0]] = o[newkey[0]];
		for (var i = newkey.length - 1; i > 0; i--) {
			newObj[newkey[i]] = o[newkey[i]];
		}
	}

	return newObj;
}

export function decryptRes(e) {
	try {
		var secretKey = localStorage.getItem('secretKey');
		var keyHex = CryptoJS.enc.Utf8.parse(secretKey);
		e = e.toString().replace(/[\r\n]/g, "");
		var decrypted = CryptoJS.TripleDES.decrypt(e, keyHex, {
			iv: CryptoJS.enc.Utf8.parse('01234567'),
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		}).toString(CryptoJS.enc.Utf8);

		return decrypted;
	}
	catch (err) {
		return (err);
	}

}

export function dealPagination(pageInfo) {
	if (pageInfo) {
		let pagination = {};
		pagination['current'] = pageInfo.pageNum;
		pagination['pageSize'] = pageInfo.pageSize;
		pagination['total'] = pageInfo.total;
		return pagination;
	} else {
		return pageInfo
	}

}


